﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using iTextSharp.text.pdf.parser;
using System.Collections;

namespace WinFormStudy
{
    public partial class Form_PicZip : Form
    {
        private WinFormStudy.JpgKit.ImgZipType ZipType;
        public Form_PicZip()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Show();
            Control.CheckForIllegalCrossThreadCalls = false;//这一行是关键
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (tbPicPath.Text.Length > 0 && tbGenPath.Text.Length > 0)
            {
                Thread t = new Thread(PicZip);
                t.Start();
            }
            else
            {
                MessageBox.Show("图片文件与生成目录不得为空", "错误");
            }
        }

        private void PicZip(object obj)
        {
            btnStart.Enabled = false;
            lbStatus.ForeColor = Color.Red;
            lbStatus.Text = "运行中";
            pbStatus.Visible = true;

            int wid = 0;
            int hei = 0;
            float percent = 100;
            int quality = 100;

            Int32.TryParse(tbWid.Text.Trim(), out wid);
            Int32.TryParse(tbHei.Text.Trim(), out hei);
            float.TryParse(nudPercent.Value.ToString(), out percent);
            Int32.TryParse(nudQuality.Value.ToString(), out quality);
            percent = percent / 100f;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            JpgKit.JpgZip(tbPicPath.Text, tbGenPath.Text, wid, hei, percent,quality, ZipType);

            stopwatch.Stop();
            btnStart.Enabled = true;
            lbStatus.ForeColor = Color.Green;
            lbStatus.Text = "已转换";
            pbStatus.Visible = false;
            lbTime.Text = stopwatch.Elapsed.ToString();
        }

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            //初始化一个OpenFileDialog类
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = @"图片文件|*.jpg;*.png;*.gif";
            //判断用户是否正确的选择了文件
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                tbPicPath.Text = fileDialog.FileName;                
            }
        }

        private void btnChooseFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == DialogResult.OK)
            {
                string path = folder.SelectedPath;
                tbGenPath.Text = path;
            }
        }

        private void Form_PicZip_Load(object sender, EventArgs e)
        {
            this.cbbZipType.SelectedIndex = 1;
        }

        private void cbbZipType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string zipType = this.cbbZipType.SelectedItem.ToString();
            switch (zipType)
            {
                case "指定宽高裁剪":
                    ZipType = JpgKit.ImgZipType.Cut;
                    break;
                case "指定宽高":
                    tbHei.Enabled = true;
                    tbWid.Enabled = true;
                    ZipType = JpgKit.ImgZipType.WH;
                    break;
                case "指定宽":
                    tbHei.Enabled = false;
                    tbWid.Enabled = true;
                    ZipType = JpgKit.ImgZipType.W;
                    break;
                case "指定高":
                    tbHei.Enabled = true;
                    tbWid.Enabled = false;
                    ZipType = JpgKit.ImgZipType.H;
                    break;
                case "指定百分比":
                    tbWid.Enabled = false;
                    tbHei.Enabled = false;
                    nudPercent.Enabled = true;
                    ZipType = JpgKit.ImgZipType.Percent;
                    break;
            }
        }


    }
}
